/********************************************************************************
* Project       : No Latency RTSP streaming Project
* Desc          : video source sender
* Author        : Seo, SeokWon 
* e-mail        : kueony@gmail.com
* Licence       : See the Copyright
*********************************************************************************/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#include <sys/socket.h> 

#include "vll_sc_protocol.h"

#include "lib_log.h"
#include "lib_network.h"
#include "vll_v4l2.h"
#include "vll_x11.h"

#define MSET(x) memset (&(x), 0, sizeof(x))


#define	CAPTURE_WIDTH		160
#define CAPTURE_HEIGHT		120
#define MAX_VIDEO_DATA_SIZE 614400

/*****************************************************************************/
/* Variables */
/*****************************************************************************/
typedef struct _vll_client_s_data
{
	st_v4l2_data	v4l2;
	st_x11_data		x11;
	char			yuv_data[MAX_VIDEO_DATA_SIZE];	
} st_vcs_data;

/*****************************************************************************/
/* Functions */
/*****************************************************************************/

int main()
{
	LibLogSetFileName((char*)"./vll_client_s.log");
	LibLogSetType(LIB_LOG_TYPE_MSG | LIB_LOG_TYPE_DATA | LIB_LOG_TYPE_ERROR | LIB_LOG_TYPE_DEBUG);
	
	st_vcs_data			vcs_data;
	int					iServSock		= 0;
	int					iConnectStatus	= 0;
//	char				ip[] = "localhost";
	char				ip[] = "131.252.214.225";
	st_ServerClientMsg	stVideoMsg;
	
	MSET(vcs_data);

	if(open_v4l2(&vcs_data.v4l2, (char*)"/dev/video0", CAPTURE_WIDTH, CAPTURE_HEIGHT) < 0){
		LogMsg(LIB_LOG_TYPE_ERROR, "(%s, %d) %s", __FILE__, __LINE__, "fail_open_cam");
		goto ERR_HANDLER;
	}
/*	
	if(create_xwindow(&vcs_data.x11, CAPTURE_WIDTH, CAPTURE_HEIGHT) < 0){
		LogMsg(LIB_LOG_TYPE_ERROR, "(%s, %d) %s", __FILE__, __LINE__, "fail_create_xwindow");
		goto ERR_HANDLER;	
	}
*/
	printf("DEBUG(%d)\n", __LINE__);	
	start_capturing(&vcs_data.v4l2);
	
	printf("start_capturing\n");
	
	// main routine
	while(1)
	{
		//get_video_data(&vcs_data.v4l2, vcs_data.x11.xImage->data, vcs_data.yuv_data);
		get_video_data(&vcs_data.v4l2, NULL, vcs_data.yuv_data);

		//draw(&vcs_data.x11);	

		if(iConnectStatus == 0)
		{
			if((iServSock = MakeSocketAndConnect(ip, 19999)) < 0)
			{
				LogMsg(LIB_LOG_TYPE_ERROR, "(%s,%d) %s", __FILE__, __LINE__, "create_socket_and_connect");
				sleep(1);
				continue;
			}
	
			iConnectStatus = 1;
		}
		else
		{			
			stVideoMsg.opcode	= VLL_SC_VIDEO_SOURCE;
			stVideoMsg.datasize	= vcs_data.v4l2.cap_data_size;
			
			if(SendMsg4FixSize(iServSock, (char *)&stVideoMsg, sizeof(stVideoMsg)) < 0)
			{
				LogMsg(LIB_LOG_TYPE_ERROR, "(%s, %d) %s", __FILE__, __LINE__, "send_video_msg1");
				SOCKET_ALL_CLOSE(iServSock);
				iConnectStatus = 0;
				continue;
			}
			
			if(SendMsg4FixSize(iServSock, (char *) vcs_data.yuv_data, stVideoMsg.datasize) < 0)
			{
				LogMsg(LIB_LOG_TYPE_ERROR, "(%s, %d) %s", __FILE__, __LINE__, "send_video_msg1");
				SOCKET_ALL_CLOSE(iServSock);
				iConnectStatus = 0;
				continue;
			}
		}
		
		usleep(100000);
	}

	if(iServSock > 0) SOCKET_ALL_CLOSE(iServSock);
	
	printf("\n");
	
	printf("close\n");
	
//	destroy_xwindow(&vcs_data.x11);
	close_v4l2(&vcs_data.v4l2);
	
	return 0;
	
ERR_HANDLER:
//	destroy_xwindow(&vcs_data.x11);
	close_v4l2(&vcs_data.v4l2);
	
	return -1;	
}


